package helper

// @Title       function.go
// @author      liqi 2018/12/17 16:48
// @Description 自定义函数库

import (
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
)

// @Title		httpSend
// @author  	liqi 2018/12/17 16:51
// @Description	发送http请求
// @Param   	url string 请求地址
// @Param   	body bytes.Reader json值

func httpSend(url string, body io.Reader, head map[string]string) (string, int) {
	//生成client 参数为默认
	client := &http.Client{}

	//提交请求
	reqest, err := http.NewRequest("GET", url, body)
	for key, item := range head {
		//reqest.Header.Set("Content-Type", "application/json; charset=utf-8")
		reqest.Header.Set(key, item)
	}

	if err != nil {
		panic(err)
	}

	//处理返回结果
	response, _ := client.Do(reqest)

	respBytes, err := ioutil.ReadAll(response.Body)
	if err != nil {
		fmt.Println(err.Error())
		return "", 400
	}
	// byte数组直接转成string，优化内存
	//str := (*string)(unsafe.Pointer(&respBytes))
	//fmt.Println("unsafe.Pointer：", *str)
	return string(respBytes), response.StatusCode

}

// @Title       ifElseReturn
// @author      liqi 2018/12/17 17:58
// @Description 简单封装三元操作符

func ifElseReturn(param1 interface{}, param2 interface{}) interface{} {
	if param1 == true {
		return param1
	}
	return param2
}
